BigDb ORM Usage

ORMs can be loaded from stored queries, raw sql queries, or through convenience getters.

Orms can insert, update, and delete database rows.

Also See: CRUD, BigOrm class, BigFormOrm class

Docs

  • BigOrm API
  • Forms
  • Inserting new ORM item

BigOrm API

To load orms, see CRUD

Other methods will be defined on the ORM subclass.

  • $item->prop to get/set values (supports magic setters/getters)
  • $item->save() will INSERT or UPDATE an item. On insert, the item's id is updated. Triggers $item->onWillSave() & onDidSave()
  • $item->delete() will delete the current item from the database, if it is stored. Returns true if a deletion occurs, false otherwise. DO NOT use the item after deletion. Triggers $item->onWillDelete() & onDidDelete()
  • $item->refresh() reloads the item's row from the database & calls $item->set_from_db($loaded_row). Throws if $item->id is not set, or if a row is not returned for the $item->id
  • $item->is_saved() returns true if the item is already in the database. It does NOT check if the current item has been modified and is different from the database version.

Forms

Some ORMs extend from \Tlf\BigFormOrm (instead of \Tlf\BigOrm), to simplify accepting user-submitted data.

  • $item->set_from_form(array $post_data, mixed $form_id = null) - intialize from user-submitted data. (the orm class should handle sanitization for you)
  • $item->sanitize(mixed $input): mixed - return strings with html removed, basically.
  • $item->slugify(string $text): string - remove all chars except alpha, numeric, and hyphen (-).

Inserting new ORM item

<?php  
  
$article = new Tlf\BigDb\Test\Article($db);  
  
$article->title = 'BigOrm has delete now!';  
$article->body = 'Saving is done, and now we have deletion!';  
$article->created_at = new \DateTime(); # normally database generated, but can be set manually  
$article->author_id = 29;  
$article->status = \MyNamespace\StatusEnum::Public;  
  
$article->save(); // $article->id is set during save()  
$article->refresh(); # Loads mysql-generated columns like `uuid` and `updated_at`